package com.wl.service.Impl;

import com.wl.mapper.CommentMapper;
import com.wl.pojo.Comment;
import com.wl.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 对文章发表评论的接口
     */
    @Override
    public Comment sendComment(Comment comment) {
        commentMapper.sendComment(comment);
        return commentMapper.getCommentInfoByCommentId(comment.getId());
    }

    /**
     * 获取文章评论
     */
    @Override
    public List<Comment> getPostCommentsByPostId(Integer postId) {
        return commentMapper.getPostCommentsByPostId(postId);
    }

    /**
     * 查询当前用户对某篇文章下面评论的点赞情况
     */
    @Override
    public List<Integer> getUserLikesComment(Integer userId, Integer postId) {
        return commentMapper.getUserLikesComment(userId, postId);
    }

    /**
     * 点赞某条评论
     */
    @Override
    @Transactional
    public void likeComment(Integer userId, Integer id) {
        // 点赞评论
        commentMapper.likeComment(userId, id);
        // 更新评论的点赞数
        commentMapper.likeCountReduce(id);
    }

    /**
     * 取消点赞某条评论
     */
    @Override
    public void cancelLikeComment(Integer userId, Integer id) {
        // 取消点赞评论
        commentMapper.cancelLikeComment(userId,id);
        // 更新评论的点赞数
        commentMapper.LikeCountIncrease(id);
    }

    /**
     * 删除某条评论
     */
    @Override
    @Transactional
    public void deleteComment(List<Integer> ids) {
        commentMapper.deleteComment(ids);
    }

}
